//
//  HWHomeDataCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/24.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

import HGCircularSlider
import Charts
class HWHomeDataCell: UICollectionViewCell {
    
    /// 新增用户背景
    var newUserBg:UIView!
    /// 新增用户底部图片
    var newUserBgImageView:UIImageView!
    /// 新增用户logo
    var newUserImageView:UIImageView!
    /// 新增用户
    var newUserTitleLabel:UILabel!
    /// 新增用户
    var newUserLabel:UILabel!
    /// 加号
    var newUserSub:UILabel!
    /// 付费转化率背景
    var conversionRateBg:UIImageView!
    /// 付费转化率logo
    var conversionRateImageView:UIImageView!
    var circularSlider:CircularSlider!
    /// 付费转化率
    var conversionRateTitleLabel:UILabel!
    /// 付费转化率
    var conversionRateLabel:UILabel!
    /// 百分号
    var conversionRatePercent:UILabel!
    /// 付费人数背景
    var payNumBg:UIImageView!
    /// 付费人数logo
    var payNumImageView:UIImageView!
    /// 付费人数
    var payNumTitleLabel:UILabel!
    /// 付费人数
    var payNumLabel:UILabel!
    /// 加号
    var payNumSub:UILabel!
    /// 充值金额背景
    var payPriceBg:UIView!
    /// 充值金额背景图片
    var payPriceBgImageView:UIImageView!
    /// 充值金额logo
    var payPriceImageView:UIImageView!
    /// 充值金额
    var payPriceTitleLabel:UILabel!
    /// 充值金额
    var payPriceLabel:UILabel!
    /// 折线标题
    var chartTitleLabel:UILabel!
    /// 折线
    var chartView:LineChartView!
    
    var model:HWHomeModel? {
        didSet {
            newUserLabel.text = model?.shuju.reg
            conversionRateLabel.text = model?.shuju.fufeilv
            if model?.shuju.fufeilv.count == 5 {
                conversionRateLabel.font = .boldSystemFont(ofSize: 25*widthScale)
            }else {
                conversionRateLabel.font = .boldSystemFont(ofSize: 34*widthScale)
            }
            payNumLabel.text = model?.shuju.pay
            payPriceLabel.text = model?.shuju.money
            circularSlider.endPointValue = CGFloat(Double(model?.shuju.fufeilv ?? "0") ?? 0.0)
            setDataCount()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWHomeDataCell {
    private func initUI() -> Void {
        newUserBg = UIView()
        newUserBg.layer.cornerRadius = 12*widthScale
        newUserBg.backgroundColor = .colorWithHexString(hexString: "#FFC83E")
        contentView.addSubview(newUserBg)
        newUserBg.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#545454", alpha: 0.15).cgColor
        newUserBg.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        newUserBg.layer.shadowOpacity = 1
        newUserBg.layer.shadowRadius = 4*widthScale
        
        newUserBgImageView = UIImageView()
        newUserBgImageView.image = UIImage(named: "addNewUserBg")
        newUserBg.addSubview(newUserBgImageView)
        
        newUserImageView = UIImageView()
        newUserImageView.image = UIImage(named: "addNewUser")
        newUserBg.addSubview(newUserImageView)
        
        newUserTitleLabel = HWLabel.createBoldLabel(title: "新增用户", color: "#FFFFFF", font: 14*widthScale)
        newUserBg.addSubview(newUserTitleLabel)
        
        newUserLabel = HWLabel.createBoldLabel(title: "12434", color: "#FFFFFF", font: 34*widthScale)
        newUserBg.addSubview(newUserLabel)
        
        newUserSub = HWLabel.createBoldLabel(title: "+", color: "#FFFFFF", font: 14*widthScale)
        newUserBg.addSubview(newUserSub)
        
        conversionRateBg = UIImageView()
        conversionRateBg.layer.cornerRadius = 12*widthScale
        conversionRateBg.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        contentView.addSubview(conversionRateBg)
        conversionRateBg.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#545454", alpha: 0.15).cgColor
        conversionRateBg.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        conversionRateBg.layer.shadowOpacity = 1
        conversionRateBg.layer.shadowRadius = 4*widthScale
        
        conversionRateImageView = UIImageView()
        conversionRateImageView.image = UIImage(named: "conversionRate")
        conversionRateBg.addSubview(conversionRateImageView)
        
        conversionRateTitleLabel = HWLabel.createBoldLabel(title: "付费转化率", color: "#FFFFFF", font: 14*widthScale)
        conversionRateBg.addSubview(conversionRateTitleLabel)
        
        conversionRateLabel = HWLabel.createBoldLabel(title: "8.6", color: "#FFFFFF", font: 34*widthScale)
        conversionRateBg.addSubview(conversionRateLabel)
        
        conversionRatePercent = HWLabel.createBoldLabel(title: "%", color: "#FFFFFF", font: 14*widthScale)
        conversionRateBg.addSubview(conversionRatePercent)
        
        circularSlider = CircularSlider.init()
        circularSlider.backgroundColor = .clear
        circularSlider.diskColor = .clear
        circularSlider.minimumValue = 0.0
        circularSlider.maximumValue = 100.0
        //        circularSlider.endPointValue = 0.086
        circularSlider.lineWidth = 6*widthScale
        circularSlider.backtrackLineWidth = 6*widthScale
        circularSlider.isUserInteractionEnabled = false
        circularSlider.thumbLineWidth = 0.0
        circularSlider.thumbRadius = 0.0
        circularSlider.roundType = .square
        circularSlider.trackColor = .colorWithHexString(hexString: "#4D6BE2")
        circularSlider.trackFillColor = .colorWithHexString(hexString: "#FFC83E")
        conversionRateBg.addSubview(circularSlider)
        
        payNumBg = UIImageView()
        payNumBg.layer.cornerRadius = 12*widthScale
        payNumBg.backgroundColor = .colorWithHexString(hexString: "#18AC67")
        contentView.addSubview(payNumBg)
        payNumBg.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#545454", alpha: 0.15).cgColor
        payNumBg.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        payNumBg.layer.shadowOpacity = 1
        payNumBg.layer.shadowRadius = 4*widthScale
        
        payNumImageView = UIImageView()
        payNumImageView.image = UIImage(named: "payNumUser")
        payNumBg.addSubview(payNumImageView)
        
        payNumTitleLabel = HWLabel.createBoldLabel(title: "付费人数", color: "#FFFFFF", font: 14*widthScale)
        payNumBg.addSubview(payNumTitleLabel)
        
        payNumLabel = HWLabel.createBoldLabel(title: "12434", color: "#FFFFFF", font: 34*widthScale)
        payNumBg.addSubview(payNumLabel)
        
        payNumSub = HWLabel.createBoldLabel(title: "+", color: "#FFFFFF", font: 14*widthScale)
        payNumBg.addSubview(payNumSub)
        
        payPriceBg = UIView()
        payPriceBg.layer.cornerRadius = 12*widthScale
        payPriceBg.backgroundColor = .colorWithHexString(hexString: "#FF7D58")
        contentView.addSubview(payPriceBg)
        payPriceBg.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#545454", alpha: 0.15).cgColor
        payPriceBg.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        payPriceBg.layer.shadowOpacity = 1
        payPriceBg.layer.shadowRadius = 4*widthScale
        
        payPriceBgImageView = UIImageView()
        payPriceBgImageView.image = UIImage(named: "payPriceBg")
        payPriceBg.addSubview(payPriceBgImageView)
        
        payPriceImageView = UIImageView()
        payPriceImageView.image = UIImage(named: "payPriceImage")
        payPriceBg.addSubview(payPriceImageView)
        
        payPriceTitleLabel = HWLabel.createBoldLabel(title: "充值金额", color: "#FFFFFF", font: 14*widthScale)
        payPriceBg.addSubview(payPriceTitleLabel)
        
        payPriceLabel = HWLabel.createBoldLabel(title: "12434", color: "#FFFFFF", font: 34*widthScale)
        payPriceBg.addSubview(payPriceLabel)
        
        chartTitleLabel = HWLabel.createBoldLabel(title: "游戏盒子下载量", color: "#333333", font: 16*widthScale)
        contentView.addSubview(chartTitleLabel)
        
        chartView = LineChartView()
        chartView.backgroundColor = .white
        contentView.addSubview(chartView)
        
        chartView.delegate = self
        
        chartView.chartDescription?.enabled = false
        chartView.dragEnabled = true
        chartView.setScaleEnabled(true)
        chartView.pinchZoomEnabled = true
        //        chartView.gridBackgroundColor = .blue
        //        chartView.drawGridBackgroundEnabled = true
        
        //        let l = chartView.legend
        //        l.form = .line
        //        l.font = UIFont(name: "HelveticaNeue-Light", size: 20)!
        //        l.textColor = .red
        //        l.horizontalAlignment = .left
        //        l.verticalAlignment = .bottom
        //        l.orientation = .horizontal
        //        l.drawInside = false
        
        chartView.legend.form = .none
        chartView.legend.textColor = .clear
        
        let xAxis = chartView.xAxis
        xAxis.labelFont = .systemFont(ofSize: 8*widthScale)
        xAxis.labelTextColor = .colorWithHexString(hexString: "#888888")
        xAxis.drawLabelsEnabled = true
        xAxis.drawAxisLineEnabled = false
        xAxis.drawGridLinesEnabled = false
        xAxis.labelPosition = .bottom
        
        
        let leftAxis = chartView.leftAxis
        leftAxis.labelTextColor = .colorWithHexString(hexString: "#888888")
        leftAxis.labelFont = .systemFont(ofSize: 12*widthScale)
        //        leftAxis.axisMaximum = 1000
        //        leftAxis.axisMinimum = 0
        leftAxis.gridColor = .colorWithHexString(hexString: "#EEEEEE")
        leftAxis.drawGridLinesEnabled = true
        leftAxis.granularityEnabled = true
        /// 竖线
        leftAxis.drawAxisLineEnabled = false
        leftAxis.drawZeroLineEnabled = true
        
        let rightAxis = chartView.rightAxis
        rightAxis.labelTextColor = .red
        //        rightAxis.axisMaximum = 900
        //        rightAxis.axisMinimum = -200
        rightAxis.granularityEnabled = false
        rightAxis.drawLabelsEnabled = false
        rightAxis.drawGridLinesEnabled = false
        rightAxis.drawAxisLineEnabled = false
        
    }
    
    private func initFrame() -> Void {
        newUserBg.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(8*widthScale)
            make.width.equalTo(161*widthScale)
            make.height.equalTo(152*widthScale)
        }
        
        newUserBgImageView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(53*widthScale)
        }
        
        newUserImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24*widthScale)
            make.left.top.equalTo(14*widthScale)
        }
        
        newUserTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(newUserImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(newUserImageView)
        }
        
        newUserLabel.snp.makeConstraints { make in
            make.left.equalTo(newUserImageView)
            make.top.equalTo(newUserImageView.snp.bottom).offset(13*widthScale)
        }
        
        newUserSub.snp.makeConstraints { make in
            make.left.equalTo(newUserLabel.snp.right)
            make.top.equalTo(newUserLabel).offset(3*widthScale)
        }
        
        conversionRateBg.snp.makeConstraints { make in
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(newUserBg)
            make.width.equalTo(161*widthScale)
            make.height.equalTo(94*widthScale)
        }
        
        conversionRateImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24*widthScale)
            make.left.top.equalTo(14*widthScale)
        }
        
        conversionRateTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(conversionRateImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(conversionRateImageView)
        }
        
        conversionRateLabel.snp.makeConstraints { make in
            make.left.equalTo(conversionRateImageView)
            make.top.equalTo(conversionRateImageView.snp.bottom).offset(7*widthScale)
        }
        
        conversionRatePercent.snp.makeConstraints { make in
            make.left.equalTo(conversionRateLabel.snp.right)
            make.top.equalTo(conversionRateLabel).offset(3*widthScale)
        }
        
        circularSlider.snp.makeConstraints { make in
            make.width.height.equalTo(40*widthScale)
            make.centerY.equalTo(conversionRateLabel)
            make.right.equalTo(-20*widthScale)
        }
        
        payNumBg.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(newUserBg.snp.bottom).offset(12*widthScale)
            make.width.equalTo(161*widthScale)
            make.height.equalTo(94*widthScale)
        }
        
        payNumImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24*widthScale)
            make.left.top.equalTo(14*widthScale)
        }
        
        payNumTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(payNumImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(payNumImageView)
        }
        
        payNumLabel.snp.makeConstraints { make in
            make.left.equalTo(payNumImageView)
            make.top.equalTo(payNumImageView.snp.bottom).offset(6*widthScale)
        }
        
        payNumSub.snp.makeConstraints { make in
            make.left.equalTo(payNumLabel.snp.right)
            make.top.equalTo(payNumLabel).offset(3*widthScale)
        }
        
        payPriceBg.snp.makeConstraints { make in
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(conversionRateBg.snp.bottom).offset(12*widthScale)
            make.width.equalTo(161*widthScale)
            make.height.equalTo(152*widthScale)
        }
        
        payPriceBgImageView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(73*widthScale)
        }
        
        payPriceImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24*widthScale)
            make.left.top.equalTo(14*widthScale)
        }
        
        payPriceTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(payPriceImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(payPriceImageView)
        }
        
        payPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(payPriceImageView)
            make.right.equalTo(-14*widthScale)
            make.top.equalTo(payPriceImageView.snp.bottom).offset(7*widthScale)
        }
        
        chartTitleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(payPriceBg.snp.bottom).offset(13*widthScale)
        }
        
        chartView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(chartTitleLabel.snp.bottom).offset(20*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
    }
    
    func setDataCount() -> Void {
        let yVals1 = (0..<(model?.shuju.down.day.count ?? 0)).map { (i) -> ChartDataEntry in
            //            let mult = range / 2
            //            let val = Double(arc4random_uniform(mult) + 50)
            return ChartDataEntry(x: Double(i), y: model?.shuju.down.num[i] ?? 0)
        }
        
        let set1 = LineChartDataSet(entries: yVals1, label: "")
        set1.axisDependency = .left
        set1.setColor(.colorWithHexString(hexString: "#5879FF"))
        //        set1.setCircleColor(.white)
        set1.circleColors = [.white]
        set1.circleHoleColor = .colorWithHexString(hexString: "#5B8FF9")
        
        set1.lineWidth = 2*widthScale
        set1.circleRadius = 3*widthScale
        set1.circleHoleRadius = 2*widthScale
        set1.fillAlpha = 1
        //        set1.fillColor = .black
        //        set1.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
        set1.drawFilledEnabled = true
        set1.drawCircleHoleEnabled = true
        set1.drawValuesEnabled = false
        let gradientColors = [UIColor.colorWithHexString(hexString: "#5A7BFF").cgColor, UIColor.colorWithAlphaHexString(hexString: "#FFFFFF", alpha: 0).cgColor] as CFArray
        //每组颜色所在位置（范围0~1)
        let colorLocations:[CGFloat] = [1.0, 0.0]
        //生成渐变色
        let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(),
                                       colors: gradientColors, locations: colorLocations)
        //将渐变色作为填充对象s
        set1.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0)
        
        
        let data: LineChartData = LineChartData.init(dataSets: [set1])
        data.setValueTextColor(.black)
        data.setValueFont(.systemFont(ofSize: 9*widthScale))
        
        let xValues = model?.shuju.down.day ?? []
        chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xValues)
        //        chartView.xAxis.labelCount = model?.shuju.down.day.count ?? 0
        chartView.xAxis.granularity = 1
        //        chartView.xAxis.axisMinimum = 0
        //        chartView.xAxis.axisMaximum = 6
        chartView.xAxis.forceLabelsEnabled = false
        chartView.xAxis.granularityEnabled = true
        chartView.extraRightOffset = 20*widthScale
        
        chartView.data = data
        //        chartView.setVisibleXRangeMaximum(3.0)
        //        chartView.setVisibleXRangeMinimum(0)
        
    }
}


extension HWHomeDataCell:ChartViewDelegate {
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        self.showMarkerView(xValue: "\(entry.x)",yValue: "\(entry.y)")
    }
    
    //显示MarkerView标签
    func showMarkerView(xValue:String,yValue:String){
        let marker = MarkerView(frame: CGRect(x: 20*widthScale, y: 20*widthScale, width: 91*widthScale, height: 42*widthScale))
        marker.chartView = self.chartView
        
        let  markBgView = UIView(frame: CGRect(x: 0, y: 0, width: 91*widthScale, height: 42*widthScale))
        markBgView.backgroundColor = .white
        markBgView.layer.cornerRadius = 12
        markBgView.layer.shadowRadius = 4
        markBgView.layer.shadowOpacity = 1
        markBgView.layer.shadowOffset = CGSize(width: 0, height: 2)
        markBgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#545454", alpha: 0.15).cgColor
        marker.addSubview(markBgView)
        
        let timeLabel = HWLabel.createBoldLabel(title: "日期：\(model?.shuju.down.day[Int(Double(xValue) ?? 0)] ?? "")", color: "#333333", font: 8*widthScale)
        markBgView.addSubview(timeLabel)
        
        let numLabel = HWLabel.createBoldLabel(title: "下载量：\(yValue)", color: "#333333", font: 8*widthScale)
        markBgView.addSubview(numLabel)
        timeLabel.frame = CGRect(x: 10*widthScale, y: 10*widthScale, width: 71*widthScale, height: 11*widthScale)
        numLabel.frame = CGRect(x: 10*widthScale, y: 21*widthScale, width: 71*widthScale, height: 11*widthScale)
        
        self.chartView.marker = marker
        
    }
}
